﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 该类是对 SketchUpAPI 中的 SUStringRef 的包装
    /// </summary>
    /// <remarks>该类型仅在本API中使用，用于获取字符串数据</remarks>
    [StructLayout(LayoutKind.Sequential, Pack = 8)]
    internal struct StringRef : IDisposable
    {
        #region API 函数
        /// <summary>
        /// 创建字符串引用
        /// </summary>
        /// <param name="stringRef"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringCreate(out IntPtr stringRef);

        /// <summary>
        /// 从utf8的字符串创建
        /// </summary>
        /// <param name="out_string_ref"></param>
        /// <param name="char_array"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringCreateFromUTF8(out IntPtr out_string_ref, [MarshalAs(UnmanagedType.LPUTF8Str)] string char_array);

        /// <summary>
        /// 释放字符串引用
        /// </summary>
        /// <param name="stringRef"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringRelease(ref IntPtr stringRef);

        /// <summary>
        /// 获取字符串长度
        /// </summary>
        /// <param name="stringRef"></param>
        /// <param name="nameLength"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringGetUTF8Length(IntPtr stringRef, out long nameLength);

        /// <summary>
        /// 设置字符串
        /// </summary>
        /// <param name="string_ref"></param>
        /// <param name="char_array"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringSetUTF8(IntPtr string_ref, [MarshalAs(UnmanagedType.LPUTF8Str)] string char_array);

        /// <summary>
        /// 获取UTF8字符串
        /// </summary>
        /// <param name="stringRef"></param>
        /// <param name="req"></param>
        /// <param name="buff"></param>
        /// <param name="ret"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringGetUTF8(IntPtr stringRef, long req, byte[] buff, out long ret);

        /// <summary>
        /// 去掉开始处的空格
        /// </summary>
        /// <param name="string_ref"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringTrimLeft(IntPtr string_ref);

        /// <summary>
        /// 去掉结尾的空格
        /// </summary>
        /// <param name="string_ref"></param>
        /// <returns></returns>
        [DllImport(ObjectBase.LIB)]
        static extern SUResult SUStringTrimRight(IntPtr string_ref);
        #endregion

        public IntPtr Id;

        /// <summary>
        /// 从字符串创建
        /// </summary>
        /// <param name="text"></param>
        public StringRef(string text)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStringCreateFromUTF8(out Id, text));
        }

        public static StringRef Empty
        {
            get
            {
                StringRef sr = new StringRef();
                SketchUpExceptionHelper.ThrowSUResult(SUStringCreate(out sr.Id));
                return sr;
            }
        }

        /// <summary>
        /// 设置内容
        /// </summary>
        /// <param name="text"></param>
        public void SetText(string text)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStringSetUTF8(Id, text));
        }

        /// <summary>
        /// 去掉开头的空格
        /// </summary>
        public void TrimStart()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStringTrimLeft(Id));
        }

        /// <summary>
        /// 去掉结尾处的空格
        /// </summary>
        public void TrimEnd()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStringTrimRight(Id));
        }

        /// <summary>
        /// 转换为对应字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStringGetUTF8Length(Id, out long nameLength));
            if (nameLength == 0) return string.Empty;

            byte[] buff = new byte[nameLength + 1];
            SketchUpExceptionHelper.ThrowSUResult(SUStringGetUTF8(Id, nameLength + 1, buff, out nameLength));
            return Encoding.UTF8.GetString(buff);
        }

        void IDisposable.Dispose()
        {
            Dispose();
        }

        /// <summary>
        /// 释放资源，并返回包含的字符串
        /// </summary>
        /// <returns>返回保存的字符串</returns>
        public string Dispose()
        {
            string str = ToString();
            SketchUpExceptionHelper.ThrowSUResult(SUStringRelease(ref Id));
            return str;
        }
    }
}